package com.sunwayworld.basemodule.coredata.pjproduct.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.coredata.pjproduct.bean.MapDTO;
import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.data.page.Pagination;
import com.sunwayworld.framework.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.coredata.pjproduct.dao.LimsProductConditionDao;
import com.sunwayworld.basemodule.coredata.pjproduct.bean.LimsProductConditionBean;
import com.sunwayworld.basemodule.coredata.pjproduct.service.LimsProductConditionService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.Map;

@Repository
@GikamBean
public class LimsProductConditionServiceImpl implements LimsProductConditionService {

    @Autowired
    private LimsProductConditionDao limsProductConditionDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsProductConditionDao getDao() {
        return limsProductConditionDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsProductConditionBean limsProductCondition = jsonWrapper.parseUnique(LimsProductConditionBean.class);
        limsProductCondition.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsProductCondition);
        return limsProductCondition.getId();
    }

    @Override
    public Page<MapDTO> queryFields(RestJsonWrapperBean wrapper) {
        List<Map<String,Object>> mapList = getDao().queryFields(wrapper.extractMapFilter());
        if (mapList.isEmpty()){
            return new Pagination<>();
        }
        List<MapDTO> mapDTOList = JsonUtils.parseList(JSONObject.toJSONString(mapList), MapDTO.class);
        return new Pagination<>(mapDTOList);
    }
}
